/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using NMock2;
using NUnit.Framework;
using OpenServers.Net.Common.Utils;

namespace OpenServers.Net.Common.FileSystem.Http {
    [TestFixture]
    public class HttpDirectoryTest : BaseNMockTest {
        private Url _url;
        private IDirectory _parent;
        private HttpDirectory _dir;

        [SetUp]
        public override void SetUp() {
            base.SetUp();
            _url = new Url("http://localhost/dir1/");
            _parent = Mocks.NewMock<IDirectory>();
            Expect.AtLeast(0).On(_parent).GetProperty("FullPath").Will(Return.Value("/"));
            _dir = new HttpDirectory(_parent, _url);
        }

        [Test]
        public void TestConstructorNoName() {
            Assert.AreEqual("dir1", _dir.Name);
            Assert.AreEqual(_parent, _dir.Parent);
        }

        [Test]
        public void TestGetObjectFile() {
            IFileSystemObject fso = _dir.GetObject("/test.txt");
            Assert.IsTrue(fso is HttpFile);
            Assert.AreEqual("test.txt", fso.Name);
            Assert.AreEqual(_dir, fso.Parent);
            Assert.AreEqual("http://localhost/dir1/test.txt", ((HttpFile)fso).Url.ToString());
            Assert.AreEqual("/dir1/test.txt", fso.FullPath);
        }

        [Test]
        public void TestGetObjectDirectory() {
            IFileSystemObject fso = _dir.GetObject("/test/");
            Assert.IsTrue(fso is HttpDirectory);
            Assert.AreEqual("test", fso.Name);
            Assert.AreEqual(_dir, fso.Parent);
            Assert.AreEqual("/dir1/test/", fso.FullPath);
            Assert.AreEqual("http://localhost/dir1/test/", ((HttpDirectory)fso).Url.ToString());
        }

        [Test]
        public void TestGetObjectDirectoryMultipleDeep() {
            IFileSystemObject fso = _dir.GetObject("/dir2/dir3/");
            Assert.IsTrue(fso is HttpDirectory);
            Assert.AreEqual("dir3", fso.Name);
            Assert.AreEqual("/dir1/dir2/dir3/", fso.FullPath);
            Assert.AreEqual("http://localhost/dir1/dir2/dir3/", ((HttpDirectory)fso).Url.ToString());
        }
    }
}
